package com.sto.stoapp.activity.administrator.system;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.sto.stoapp.StoApplication;
import com.sto.stoapp.activity.R;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.listener.StoOnCheckedChangeListener;
import com.sto.stoapp.listener.StoOnClickListener;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.StoHhmEditText;
import com.sto.stoapp.widget.StoHhmEditText.EditClickListener;
import com.sto.stoapp.widget.StoHomeActivity;

import sto.android.app.StoJNI;

public class SystemScanHeaderActivity extends StoHomeActivity {

    private RadioGroup scanRadio;

    private RadioButton autoRadio, handRadio;

    private StoHhmEditText scanTimeEdit;

    private Button save;

    private final int maxTime = 25;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scan_header_settings);
        String broadType = getIntent().getStringExtra(
                HomeBroadAction.BROAD_TYPE);
        // 判断是管理员的系统设置还是业务内部的系统设置
        if (broadType != null && !broadType.equals("")) {
            this.setTitleBottom(R.string.scanner_settings, broadType);
        } else {
            this.setTitleBottom(R.string.scanner_settings,
                    HomeBroadAction.ADMINISTRATOR_BROAD);
        }
        initWidget();
        initListener();
    }

    private void initListener() {
        scanTimeEdit.setEditDelete(new EditClickListener() {

            @Override
            public void onEditDelete() {
                SystemScanHeaderActivity.this.finish();
            }

            @Override
            public void onEditClickListener(View v) {
            }
        });

        scanRadio.setOnCheckedChangeListener(new StoOnCheckedChangeListener(
                this) {

            @Override
            public void stOnCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.auto_trigger:
                        StoFilePreference
                                .getInstance(SystemScanHeaderActivity.this)
                                .setScanHeaderTrigger(true);
                        break;
                    case R.id.hand_trigger:
                        StoFilePreference
                                .getInstance(SystemScanHeaderActivity.this)
                                .setScanHeaderTrigger(false);
                        break;
                    default:
                        break;
                }
            }

        });

        save.setOnClickListener(new StoOnClickListener(this) {

            @Override
            public void mHonClick(View view) {
                // TODO Auto-generated method stub
                String time = scanTimeEdit.getText().toString().trim();
                if (time.length() > 0 && Helper.isNumeric(time)) {
                    setScanTime(Integer.valueOf(time));
                } else {
                    Helper.showToast(SystemScanHeaderActivity.this, getText(R.string.input_must_number)
                            .toString());
                }
            }
        });
    }

    /**
     * 设置扫描头时间
     *
     * @param timer
     */
    private void setScanTime(int timer) {
        StoJNI jni = StoApplication.getStoJni();
        jni.SetScannerOn();
        jni.SetScannerTimerOut(timer);
        jni.SetScannerOff();
        StoFilePreference.getInstance(this).setScanHeaderTime(timer);
        Helper.showToast(SystemScanHeaderActivity.this, getText(R.string.set_success)
                .toString());
        finish();
    }

    private void initWidget() {
        scanRadio = (RadioGroup) findViewById(R.id.scan_radio);
        autoRadio = (RadioButton) findViewById(R.id.auto_trigger);
        handRadio = (RadioButton) findViewById(R.id.hand_trigger);
        scanTimeEdit = (StoHhmEditText) findViewById(R.id.scan_time_edit);
        save = (Button) findViewById(R.id.scan_save);
        if (StoFilePreference.getInstance(this).getScanHeaderTrigger()) {
            autoRadio.setChecked(true);
        } else {
            handRadio.setChecked(true);
        }
        scanTimeEdit.setText(""
                + StoFilePreference.getInstance(this).getScanHeaderTime());
        scanTimeEdit.setSelection(scanTimeEdit.getText().toString().length());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
